package com.pp.mariage.server;

import java.math.BigDecimal;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
 * 
 * @author Pascal
 *
 */
public class ParticipationService {

	public static BigDecimal getTotalByCadeau(CadeauEnum cadeauInfo) {
		PersistenceManager pm = CadeauServlet.pmfInstance
				.getPersistenceManager();

		Query query = pm.newQuery(Paiement.class);

		query.setFilter("cadeau == cadeauParam");
		query.declareParameters("com.pp.mariage.server.CadeauEnum cadeauParam");

		@SuppressWarnings("unchecked")
		List<Paiement> paiements = (List<Paiement>) query.execute(cadeauInfo);

		BigDecimal somme = BigDecimal.ZERO;

		for (Paiement paiement : paiements) {
			if (paiement.getMontant() != null) {
				somme = somme.add(paiement.getMontant());
			}
		}
		return somme;
	}

	public static boolean isCadeauPris(CadeauEnum cadeauInfo) {
		PersistenceManager pm = CadeauServlet.pmfInstance
				.getPersistenceManager();

		Query query = pm.newQuery(Paiement.class);

		query.setFilter("cadeau == cadeauParam");
		query.declareParameters("com.pp.mariage.server.CadeauEnum cadeauParam");

		@SuppressWarnings("unchecked")
		List<Paiement> paiements = (List<Paiement>) query.execute(cadeauInfo);
		if (!paiements.isEmpty()) {
			return true;
		}
		return false;
	}
}
